<?php

namespace App\Services\MockUser;

use App\Models\GoldEnd;
use App\Models\GoldRound;
use App\Models\GoldProduct;
use App\Jobs\RobotTerminateJob;
use Illuminate\Database\Eloquent\Collection;

class StormTerminator extends MockUser
{
	protected $round;

	public function __construct(GoldRound $round)
	{
		$this->round = $round;
	}

	protected function name(): string
	{
		return 'terminator';
	}

	protected function phone(): string
	{
		return '18800000002';
	}

	protected function create(GoldEnd $end)
	{
        $queue = app()->environment('local') ? 'default' : 'robot_terminate_record';
        dispatch(new RobotTerminateJob($this->robot(), $end))->onQueue($queue);
	}

	protected function end()
	{
        GoldEnd::query()
            ->where('gold_round_id', $this->round->id)
            ->whereNull('ended_at')
            ->chunks(50, function (Collection $ends) {

            if ($ends->isEmpty()) return false;

            foreach ($ends as $end) {
                if (sc('is_terminate_on')) {
                    $this->create($end);
                }
            }

            return true;
        });
	}

	public function terminate()
	{
        $this->end();
	}
}
